package sdk.webview.fmc.com.fmcsdk.presenter;


import android.os.Environment;
import android.preference.PreferenceManager;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import sdk.webview.fmc.com.fmcsdk.base.BaseData;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.base.UserApi;
import sdk.webview.fmc.com.fmcsdk.interfaces.UploadLogView;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.SdkVersionUtils;
import sdk.webview.fmc.com.fmcsdk.util.Url;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ResponseTransformer;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber.ResponseSubscriber;

/**
 * Created by ydm on 2018/6/8.
 */

public class UploadLogPresenter extends BasePresenter<UploadLogView> {

    public void uploadLog(String fileName, final int i){
        String storePath;
        if (SdkVersionUtils.checkedAndroid_R()) {
            storePath =view.getViewContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(view.getViewContext()).getString(Constant.APP_NAME, "fmsdk");
        } else {
            storePath = view.getViewContext().getFilesDir().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(view.getViewContext()).getString(Constant.APP_NAME, "fmsdk");

        }
//        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(view.getViewContext()).getString(Constant.APP_NAME, "fmsdk");
        final File files = new File(storePath+Constant.LOGPATH+"/"+fileName);
//        File files = new File(Constant.IMAGEPATH+"/"+Constant.SRCAVATAR);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data;charset=utf-8"), files);
        getApi(UserApi.class).uploadLog(getDefaultAddress()+ Url.UPLOAD_LOG,  Url.XMLHTTPREQUEST,UserHelper.getToken(),requestBody)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        view.uploadLogSuccess(files,i);

                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                    }
                    @Override
                    public void onRequestAgain() {
                    }
                });

    }

//    File file = new File(Constant.LOGPATH+"/"+fileName);
//    if (!file.exists()){
//        XLog.i("没有改文件");
//        return;
//    }
//    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//
//    MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestBody);
//
//
//    getApi(UserApi.class).uploadLog(getDefaultAddress()+ Url.UPLOAD_LOG, UserHelper.getToken(),requestBody,body)
//            .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
//            .subscribe(new ResponseSubscriber<BaseData>(view) {
//        @Override
//        public void success(BaseData baseData) {
//            XLog.i(baseData.toString());
//        }
//        @Override
//        public void onError(Throwable e) {
//            super.onError(e);
//
//        }
//        @Override
//        public void onRequestAgain() {
//        }
//    });
}
